import { createRouter, createWebHashHistory } from 'vue-router';
import { showToast } from 'vant';
// 路由规则
const routes = [
    {
        path: '/',
        component: () => import(/*webpackChunkName:"chunk-01"*/'../views/LayoutView'),
        redirect: '/home',
        meta: {
            requireLogin: false
        },
        children: [
            {
                name: 'home',
                path: '/home',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/HomeView'),
                meta: {
                    title: '商城首页',
                    requireLogin: false
                }
            },

            {
                name: 'cate',
                path: '/cate',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/CateView'),
                meta: {
                    title: '商品分类',
                    requireLogin: false
                }
            },

            {
                name: 'ucenter',
                path: '/ucenter',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/UcenterView'),
                meta: {
                    title: '会员中心',
                    requireLogin: true
                }
            },


            {
                name: 'cart',
                path: '/cart',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/CartView'),
                meta: {
                    title: '购物车',
                    requireLogin: true
                }
            },
            {
                name: 'addr',
                path: '/addr',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/AddrView'),
                meta: {
                    title: '地址管理',
                    requireLogin: true
                }
            },
            {
                name: 'addrAdd',
                path: '/addrAdd',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/AddrAdd'),
                meta: {
                    title: '新增/编辑地址',
                    requireLogin: true
                }
            },
            {
                name: 'goods',
                path: '/goods',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/GoodsView'),
                meta: {
                    title: '商品详情',
                    requireLogin: false
                }
            },
            {
                name: 'search',
                path: '/search',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/SearchView'),
                meta: {
                    title: '商品详情',
                    requireLogin: false
                }
            },
            {
                name: 'goodsList',
                path: '/goodsList',
                component: () => import(/*webpackChunkName:"chunk-01"*/'../views/GoodsList'),
                meta: {
                    title: '商品列表',
                    requireLogin: false
                }
            },
        ]
    },
    {
        name: 'login',
        path: '/login',
        component: () => import(/*webpackChunkName:"chunk-02"*/'../views/LoginView'),
        meta: {
            title: '会员登录',
            requireLogin: false
        }
    },
    {
        name: 'register',
        path: '/register',
        component: () => import(/*webpackChunkName:"chunk-02"*/'../views/RegView'),
        meta: {
            title: '会员注册',
            requireLogin: false
        }
    },


];
// 创建路由实例
const router = createRouter({
    routes,
    history: createWebHashHistory()
});
// 路由前置导航守卫
router.beforeEach((to, from, next) => {
    if (to.meta.requireLogin) {
        // 检测登录状态
        const { token } = JSON.parse(localStorage.getItem('userLoginInfo') || '{}');
        if (!token) {
            showToast('您还没有登录');
            return next({ name: "login",query:{url:to.fullPath} });
        } else {
            next();
        }
    } else {
        next();
    }

    if (to.meta.title) {
        document.title = to.meta.title;
    }
});
export default router;